

#include <cegame/ceeffectloader.h>
#include <cegfx/ceeffect.h>
#include <d3dx9.h>

ceEffectLoader::ceEffectLoader ()
{
}

bool ceEffectLoader::CanLoad (iFile* file) const
{
	ceString ext = file->GetExtension ();
	if (	ext == "fx")
	{
		return true;
	}
	return false;
}


ceEffect* ceEffectLoader::Load (LPDIRECT3DDEVICE9 device, iFile* file)
{
	file->Seek (iFile::SP_End);
	size_t length = file->Tell ();
	file->Seek (iFile::SP_Set);

	char* buffer = new char[length];
	size_t read = file->Read (buffer, length);
	if (read != length)
	{
		delete []  buffer;
		return 0;
	}

	LPD3DXEFFECT effect = 0;
	LPD3DXBUFFER errorBuf = 0;
	if (FAILED (D3DXCreateEffect (
		device,
		buffer,
		length, 
		NULL,
		NULL,
		0,
		NULL,
		&effect,
		&errorBuf)))
	{
		delete [] buffer;

		buffer = new char [errorBuf->GetBufferSize()+1];
		memcpy (buffer, errorBuf->GetBufferPointer(), errorBuf->GetBufferSize ());
		buffer[length] = '\0';
		printf (buffer);
		delete [] buffer;

		return 0;
	}

	delete [] buffer;

	ceEffect* ceeffect = new ceEffect ();
	ceeffect->SetEffect (effect);

	return ceeffect;
}

